function [ obj ] = plotSupplyCurve(obj, auction_id, s, strategy, risk_scenario, ceiling_scenario, filename, optfig)
% This function plots the  supply and cost curves by clearing auctions
% under different bidding strategies and scenaiors.
%
%   INPUTS
%       obj: bidCounter object (self)
%       auction_id: (string) identifier for auction
%       s: (int 1<=s<=200) simulation number from which to get bids 
%       strategy: (struct) strategy for bidding in ceiling auctions
%       risk_scenario: (string) Risk scenario to inflate bids with
%       ceiling_scenario: (string)

%% Setup

% Set strategy
obj.strategy = strategy;

% Set scenario index 
R = length(obj.cprisk);
r = find(strcmp(obj.cprisk,risk_scenario));
c = find(strcmp(obj.ceiling,ceiling_scenario));
obj.scenarioIdx = (c-1)*R + r;

% Get auction 
a = find(strcmp(obj.auction_ids,auction_id));
assert(~isempty(a),'Auction ID not found!');

% Get auction observables
ceil_exists = ~isnan(obj.ceil_cf(a,c));
ceil_price = obj.ceil_cf(a,c);
QD = obj.cfsamp.capacity_sought_mw( obj.ia(a) ); 

% Get original bids and correct for risk level
risk_factor = obj.risk_adj(a,r);
bq_orig = obj.bid_model.bids(obj.is{a,s},:);
bq_orig(:,1) = bq_orig(:,1) * risk_factor;

% Sort bids in ascending order and accumulate quantities
[bq_orig, idx] = sortrows(bq_orig,1);
bq_orig(:,2) = cumsum(bq_orig(:,2));
bq_orig = [bq_orig(1,1) 0; bq_orig];

% Get costs and correct for risk level
costs = obj.bid_model.ch(obj.is{a,s},:) * risk_factor;
costs = costs(idx);
costs = [costs(1); costs];

% Clear auction and collect submitted (modified) bids 
[~, cq] = obj.clearOne(a,s);
cq = sortrows(cq,3);
QA = sum(cq(:,2));
num_submitted = length(cq(:,1));

% Shaded bids
cq_cleared = cq(:,3:4);
cq_cleared(:,2) = bq_orig(1:num_submitted,2);
if ~isempty(cq_cleared)
    cq_cleared = [cq_cleared(1,1) 0; cq_cleared];
end
%% Plot

% Compute axis limits
ymax = ceil(nanmax(max(bq_orig(:,1)),ceil_price));
ymin = floor(nanmin(min(costs),ceil_price));
xmax = max(ceil(max(bq_orig(:,2))/100)*100,QD);
xmin = 0;
if xmin == xmax
    xmin = xmax - 10;
end

% Lines
f0 = figure;
xline(QD,'--','Color',[0 0 0],'LineWidth',optfig.axisweight-0.5);
text(QD-0.05*(xmax - xmin), ymin + 0.01*(ymax-ymin),'$Q_D$','Interpreter','latex','HorizontalAlignment','center',...
    'VerticalAlignment','bottom',optfig.labProp{:});
xline(QA,'--','Color',[0 0 0],'LineWidth',optfig.axisweight-0.5);
text(QA+0.05*(xmax-xmin), ymin + 0.01*(ymax-ymin),'$Q_A$','Interpreter','latex','HorizontalAlignment','center',...
    'VerticalAlignment','bottom',optfig.labProp{:});
if ceil_exists
    pct = (QD < xmax)*0.05 - (QD == xmax)*0.05;
    base = (QD < xmax)*xmin + (QD == xmax)*xmax;
    yline(ceil_price,'--','Color',[0 0 1],'LineWidth',optfig.axisweight-0.5);
    text(base + pct*(xmax - xmin), ceil_price + 0.025*(ymax-ymin),'$r$','Interpreter','latex','HorizontalAlignment','center',...
    'VerticalAlignment','bottom',optfig.labProp{:});  
end
hold on;

% Step functions
s1 = stairs(bq_orig(:,2),bq_orig(:,1),'Color',[0 0 0], 'LineWidth', optfig.axisweight-0.5); % Bid curve
hold on;
s2 = stairs(bq_orig(:,2),costs,'Color',[1 0 0], 'LineWidth', optfig.axisweight-0.5); % Cost curve
hold on;
s3 = stairs(cq_cleared(:,2), cq_cleared(:,1), 'Color',[0 0 0], 'LineWidth', optfig.axisweight+2); % Cleared bids (modified by strategy)

% Figure formatting
xlim([xmin xmax]);
ylim([ymin ymax]);
xstep = (xmax-xmin)/10;
ystep = (ymax - ymin)/4;

set(gcf, 'Color'       , 'w', ...
    'position',[0 0 500 500]);
set(gca, optfig.labProp{:}, ...
         'Box'         , 'off'              , ...
         'LineWidth'   ,  optfig.axisweight , ...
         'YTick'       , [ymin:ystep:ymax], ...
         'XTick'       , [xmin:xstep:xmax]);

% Axis labels
xlabel('Quantity (MW)',optfig.textProp{:});
ylabel('Price (INR/kWh)',optfig.textProp{:});

% Title
titleString = sprintf('Auction: %s, Strategy: %s',...
    auction_id,strategy.printname);
title(titleString,optfig.textProp{:},'Interpreter','latex');

% Legend
legend([s1, s3, s2] ,{'Eqb. bids','Shaded bids','Costs'},...
    'Location','southoutside',...
    'Orientation','vertical',...
     optfig.legProp{:});
  
legend boxoff;
hold off;

%% Export figure to file

% Crop whitespace 
fig = gcf;
fig.PaperPositionMode = 'auto';
fig_pos = fig.PaperPosition;
fig.PaperSize = [fig_pos(3) fig_pos(4)];

ax = gca;
outerpos = ax.OuterPosition;
ti = ax.TightInset;
left = outerpos(1) + ti(1);
bottom = outerpos(2) + ti(2);
ax_width = outerpos(3) - ti(1) - ti(3);
ax_height = outerpos(4) - ti(2) - ti(4);
ax.Position = [left bottom ax_width ax_height];

% Write to disk
fprintf(1,'Writing %s to file ...\n',filename);
print(f0,'-dpdf','-painters','-noui','-r600', filename);

end